package com.first.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.TbBottomArticle;
import com.first.common.vo.Result;
import com.first.system.dao.BottomDao;
import com.first.system.service.BottomService;
import com.first.system.vo.BottomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: first-project
 * @description:
 * @author: 闫锦超
 * @create: 2023-05-26 18:24
 **/
@Service
public class BottomServiceImpl implements BottomService {
    @Autowired
    private BottomDao bottomDao;

    @Override
    public Result getAllBottomArticle(Integer currentPage, Integer pageSize, BottomVo bottomVo) {
        Page<TbBottomArticle> page = new Page<>(currentPage, pageSize);
        QueryWrapper<TbBottomArticle> wrapper = new QueryWrapper<>();
        ////没有被删除的父目录
        wrapper.eq("is_deleted", 0);
        wrapper.eq("parent_id", 0);
        if (StringUtils.hasText(bottomVo.getName())) {
            wrapper.like("name", bottomVo.getName());
        }
        if (StringUtils.hasText(bottomVo.getIsDisable())) {
            wrapper.eq("is_disable", bottomVo.getIsDisable());
        }
        Page<TbBottomArticle> page1 = bottomDao.selectPage(page, wrapper);

        //为page集合中的records设置子类
        List<TbBottomArticle> records = page1.getRecords();
        //没有被删除的所有目录
        QueryWrapper<TbBottomArticle> childrenWrapper = new QueryWrapper<>();
        childrenWrapper.eq("is_deleted", 0);
        List<TbBottomArticle> list = bottomDao.selectList(childrenWrapper);
        //遍历records集合，设置孩子
        for (TbBottomArticle item : records) {
            List<TbBottomArticle> children = getChildren(item, list);
            item.setChildren(children);
        }
        return Result.success(page1);
    }

    @Override
    public Result changeStatus(String id, Integer isDisable) {
        TbBottomArticle tbBottomArticle = new TbBottomArticle();
        tbBottomArticle.setId(id);
        tbBottomArticle.setIsDisable(isDisable);
        int i = bottomDao.updateById(tbBottomArticle);
        if (i != 0) {
            return Result.success();
        }
        return null;
    }

    @Override
    public Result deleteBottomArticle(TbBottomArticle bottomArticle) {
        bottomArticle.setIsDeleted(1);
        int i = bottomDao.updateById(bottomArticle);
        if (i != 0) {
            return Result.success();
        }
        return null;
    }

    @Override
    public Result addBottomArticle(TbBottomArticle tbBottomArticle) {
        TbBottomArticle tbBottomArticle1 = new TbBottomArticle(IdUtil.getSnowflakeNextId() + "", tbBottomArticle.getName(), 0, 0, tbBottomArticle.getParentId(), LocalDateTime.now(), LocalDateTime.now(), new ArrayList<TbBottomArticle>());
        int insert = bottomDao.insert(tbBottomArticle1);
        if (insert != 0) {
            return Result.success();
        }
        return null;
    }

    @Override
    public Result editBottomArticle(TbBottomArticle tbBottomArticle) {
        int i = bottomDao.updateById(tbBottomArticle);
        if(i!=0){
            return Result.success();
        }
        return null;
    }

    private List<TbBottomArticle> getChildren(TbBottomArticle item, List<TbBottomArticle> childrens) {
        ArrayList<TbBottomArticle> list = new ArrayList<>();
        for (TbBottomArticle children : childrens) {
            if (item.getId().equals(children.getParentId())) {
                list.add(children);
            }
        }
        for (TbBottomArticle a : list) {
            a.setChildren(getChildren(a, childrens));
        }
        return list;
    }


}
